package com.sys.mapper;

import com.sys.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;

/**
 * 用户数据访问接口
 */
@Mapper
public interface UserMapper {

        /**
         * 根据用户名（手机号）查询用户信息
         *
         * @param username 用户名（手机号）
         * @return 用户实体对象，如果不存在则返回 null
         */
        @Select("SELECT user_id, phone, password_hash, user_type, created_at, updated_at FROM Users WHERE phone = #{username}")
        @Results({
                        @Result(property = "userId", column = "user_id"),
                        @Result(property = "passwordHash", column = "password_hash"),
                        @Result(property = "userType", column = "user_type"),
                        @Result(property = "createdAt", column = "created_at"),
                        @Result(property = "updatedAt", column = "updated_at")
        })
        User findByUsername(@Param("username") String username);

        /**
         * 插入一个新用户
         *
         * @param user 用户实体对象
         * @return 影响的行数
         */
        @Insert("INSERT INTO Users(phone, name, password_hash, user_type, created_at, updated_at) " +
                        "VALUES(#{phone}, #{name}, #{passwordHash}, #{userType}, #{createdAt}, #{updatedAt})")
        @Options(useGeneratedKeys = true, keyProperty = "userId")
        int insert(User user);
}